package woaini.wind.ai.controller;

import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONObject;
import dev.langchain4j.agent.tool.P;
import dev.langchain4j.agent.tool.Tool;
import io.micrometer.observation.annotation.Observed;
import org.springframework.stereotype.Component;

import java.time.LocalTime;
import java.util.HashMap;
import java.util.Map;

@Component
public class AssistantTools {

  public static final Map<String,String> DATASET_MAP;

  static {
    DATASET_MAP = new HashMap<>();
    DATASET_MAP.put("售后知识库","5e2925bc-e9ec-4bc9-8c9e-c779a4d3dd49");
    DATASET_MAP.put("售前知识库","79be9b59-d2d2-4738-a3dc-c6056c9aef14");
    DATASET_MAP.put("基础知识库","85acf8c4-308b-46ef-ba6d-64ad53d3772a");
  }

  /**
   * This tool is available to {@link Assistant}
   */
  @Tool(name = "获取当前时间")
  @Observed
  public String currentTime() {
    return LocalTime.now().toString();
  }

  @Tool(value = "根据知识库名称和询问问题,进行知识库召回", name = "知识库召回")
  public DatesetResponse flowRecall(@P(value = "知识库名称") String knowledgeBaseName,@P(value = "询问问题") String question) {


    //获取知识库id
    String datasetId = DATASET_MAP.get(knowledgeBaseName);

    String requestBody = STR."""
    {
      "query": "\{question}",
      "retrieval_model": {
          "search_method": "hybrid_search",
          "reranking_enable": true,
          "reranking_mode": "reranking_model",
          "reranking_model": {
              "reranking_provider_name": "langgenius/xinference/xinference",
              "reranking_model_name": "jina-reranker-v2"
          },
          "weights": null,
          "top_k": 3,
          "score_threshold_enabled": false,
          "score_threshold": null
      }
    }
    """;
    String body = HttpRequest.post(STR."http://10.8.1.250/v1/datasets/\{datasetId}/retrieve")
      .header("Authorization", "Bearer dataset-XW081TaNEJ1SzCOjCKmzDNER")
      .body(requestBody).execute().body();

    return JSONObject.parseObject(body, DatesetResponse.class);
  }

  @Tool(value = "向业务平台提交同步订单的请求", name = "提交订单同步请求")
  public OpenActionResponse submitOrderSync(@P("订单号  必须 格式为\"DD\"开头后面跟19位数字") String orderNo){
    OpenActionRequest openActionRequest = new OpenActionRequest("submit_order_sync");
    openActionRequest.setParams("orderNo",orderNo);

    String jsonString = JSONObject.toJSONString(openActionRequest);

    String body = HttpRequest.post("https://opentest.mall.chain-idea.com/api/openDis/action")
      .body(jsonString).execute().body();
    return JSONObject.parseObject(body, OpenActionResponse.class);
  }

  @Tool(value = "向业务平台获取泰滑卡签署情况,没签署的话会返回新的签署链接", name = "获取泰滑卡签署情况")
  public OpenActionResponse submitThk(@P("订单号  必须 格式为\"RNO\"开头后面跟19位数字") String orderNo){
    OpenActionRequest openActionRequest = new OpenActionRequest("submit_thk_sync");
    openActionRequest.setParams("orderNo",orderNo);

    String jsonString = JSONObject.toJSONString(openActionRequest);

    String body = HttpRequest.post("https://opentest.mall.chain-idea.com/api/openDis/action")
      .body(jsonString).execute().body();
    return JSONObject.parseObject(body, OpenActionResponse.class);
  }
}